const { createBooks, findBooks, findBooksId, updateBook, deleteBookById, deleteBooksByIdsWithTransaction } = require("../service/books.server")
const { borrowBook, borrowingInformation, updatebr, cs, ebb, rbb, rl, wl, ttb, yq,searchborrow } = require("../service/BorrowRecord.server")
// const { createBooks, findBooks, updateBook, deleteBookById ,deleteBooksByIdsWithTransaction} = require("../servi
class Borrow {

    // 借书
    async borrowrecord(ctx, next) {
        const { id, borrowerInfo } = ctx.request.body
        console.log(id, borrowerInfo, 222)
        // console.log(id,borrowerInfo,15)
        const book = await findBooksId(id)
        // 查找书籍
        if (!book) {
            ctx.body = {
                code: 400,
                message: "书籍未找到",
            }
        }
        // 检查书籍是否已借出
        if (book[0].dataValues.is_borrowed) {
            ctx.body = {
                code: 400,
                message: "书籍已被借出",
            }

        } else {
            // // 添加借书记录
            const borrowData = { ...borrowerInfo, "book_id": id }
            console.log(borrowData, 444)
            try {
                const borrowbooker = await borrowBook(borrowData)
                const upDatabooker = await updateBook(id, { "is_borrowed": 1 })
                if (borrowbooker) {
                    ctx.body = {
                        code: 200,
                        message: "借书成功",
                    }
                    console.log("借书成功")
                }
            } catch (error) {
                console.log(error, 33)
            }

        }

    }
    // 还书信息查询
    async returnBook(ctx, next) {
        const { term } = ctx.request.body
        const br = await borrowingInformation(term)
        ctx.body = {
            code: 200,
            message: "还书操作",
            term: br
        }
    }
    // 还书
    async rtb(ctx, next) {
        const { data } = ctx.request.body
        console.log(22, data)
        // console.log(id,borrowerInfo,15)
        const book = await findBooksId(data.book_id)
        // 查找书籍
        if (!book) {
            ctx.body = {
                code: 400,
                message: "书籍未找到",
            }
        }
        console.log(book[0].is_borrowed, 333)
        if (book[0].is_borrowed) {
            try {
                const upDatabooker = await updateBook(data.book_id, { "is_borrowed": 0 })
                const upDatarb = await updatebr(data.record_id, {
                    "is_borrowed": 0,
                    "return_date": data.return_date
                })
                console.log(upDatabooker, 222)
                if (upDatabooker & upDatarb) {
                    ctx.body = {
                        code: 200,
                        message: "还书成功",
                    }
                    console.log("还书成功")
                }
            } catch (error) {
                console.log(error, 33)
            }
        } else {
            ctx.body = {
                code: 400,
                message: "该书籍已归还",
            }
        }
        // // 检查书籍是否已借出
        // if (book[0].dataValues.is_borrowed) {
        //     ctx.body = {
        //         code: 400,
        //         message: "书籍已归还",
        //     }
        // } else {
        //     try {
        //         const upDatabooker = await updateBook(data[0].book_id, { "is_borrowed": 0 })
        //         console.log(upDatabooker,222)
        //         if (upDatabooker) {
        //             ctx.body = {
        //                 code: 200,
        //                 message: "借书成功",
        //             }
        //             console.log("借书成功")
        //         }
        //     } catch (error) {
        //         console.log(error, 33)
        //     }

        // }
    }

    // 分类统计
    async cs(ctx) {
        try {
            const cser = await cs()
            ctx.body = {
                code: 200,
                message: cser,
            }
        } catch (error) {
            ctx.body = {
                code: 400,
                message: "统计失败",
            }
        }


    }
    // 每天借书情况统计
    async ebb(ctx) {
        try {
            const cser = await ebb()
            ctx.body = {
                code: 200,
                message: cser,
            }
        } catch (error) {
            ctx.body = {
                code: 400,
                message: "统计失败",
            }
        }

    }
    // 每天还书情况统计
    async rbb(ctx) {
        try {
            const cser = await rbb()
            ctx.body = {
                code: 200,
                message: cser,
            }
        } catch (error) {
            ctx.body = {
                code: 400,
                message: "统计失败",
            }
        }

    }
    // 读书榜单
    async rl(ctx) {
        try {
            const cser = await rl()
            ctx.body = {
                code: 200,
                message: cser,
            }
        } catch (error) {
            ctx.body = {
                code: 400,
                message: "统计失败",
            }
        }

    }

    // 书籍受欢迎统计
    async wl(ctx) {
        try {
            const cser = await wl()
            ctx.body = {
                code: 200,
                message: cser,
            }
        } catch (error) {
            ctx.body = {
                code: 400,
                message: "统计失败",
            }
        }

    }
    // 书籍受欢迎统计
    async ttb(ctx) {
        try {
            const cser = await ttb()
            ctx.body = {
                code: 200,
                message: cser,
            }
        } catch (error) {
            ctx.body = {
                code: 400,
                message: "统计失败",
            }
        }

    }
    // 逾期
    async yq(ctx) {
        try {
            const cser = await yq()
            ctx.body = {
                code: 200,
                message: cser,
            }
        } catch (error) {
            ctx.body = {
                code: 400,
                message: "统计失败",
            }
        }

    }
    async searchborrow(ctx){
        const { limit,offset} = ctx.request.body
        // console.log(222,page,pageSize)
        //          ctx.body = {
        //             code: 200,
        //             message: 1,
        //         }
            try {
                const res = await searchborrow(limit,offset )
                ctx.body = {
                    code: 200,
                    message: res,
                }
            } catch (error) {
                console.log("error", error)
            }
    }
}


module.exports = new Borrow();